/**
 * 
 */
package demo.tellurium.test.cqp;

import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import demo.tellurium.framework.BaseTelluriumJUnitTestCase;
import demo.tellurium.module.cqp.CQPAllDocumentsModule;
import demo.tellurium.module.cqp.CQPLoginModule;
import demo.tellurium.module.cqp.CQPProfileModule;

/**
 * @author roger.xu
 * 
 */
public class TreeGridBlankJUnitTestCase extends BaseTelluriumJUnitTestCase {
    private static CQPLoginModule cqpLogin;
    private static CQPAllDocumentsModule cqpAllDocuments;
    private static CQPProfileModule cqpProfile;

    private static final int INFINITE_RUN_TIMES = -1;
    private static final int MAX_RUN_TIMES = 9999;

    @BeforeClass
    public static void initUi() {
        cqpLogin = new CQPLoginModule();
        cqpLogin.defineUi();

        cqpAllDocuments = new CQPAllDocumentsModule();
        cqpAllDocuments.defineUi();

        cqpProfile = new CQPProfileModule();
        cqpProfile.defineUi();
    }

    @Before
    public void delay() {
        waitingDelay();
    }

    @Test
    public void connectTo() {
        String contextUrl = getProperty("cqp.context.url");
        String pagePath = getProperty(CQPAllDocumentsModule.PATH_KEY);
        String url = String.format("%s/%s", contextUrl, pagePath);
        connectUrl(url);
    }

    @Test
    public void testLogin() {
        console.debug("[testLogin]");
        // cqpLogin.doAdminLogin();
        String username = getProperty("cqp.login.username");
        String password = getProperty("cqp.login.password");
        cqpLogin.doLogin(username, password);
        pauseSec(3);
    }

    @Test
    public void testReproduceTreeGridBlank() {
        console.debug("[testReproduceTreeGridBlank]");
        run();
    }

    private void run() {
        int totalTimes = INFINITE_RUN_TIMES;

        if (totalTimes == INFINITE_RUN_TIMES) {
            totalTimes = MAX_RUN_TIMES;
        }

        for (int i = 1; i <= totalTimes; i++) {
            console.debug("run:", i, "/", totalTimes);
            validateTreeGrid();
            clickTreeGridLink();
        }
    }

    private void validateTreeGrid() {
        pauseSec(3);
        console.debug("[validateTreeGrid]");
        int searchResults = cqpAllDocuments.getSearchResults();
        console.debug("searchResults:", searchResults);
        Assert.assertTrue(searchResults > 0);
    }

    private void clickTreeGridLink() {
        console.debug("[clickTreeGridLink]");
        cqpAllDocuments.doClickDocument(1);
        cqpProfile.doClickCancelButton();
    }
}
